import json
import sys
sys.path.append("../")

from services.ZhangTingService import *
from frameworks.utils.RedisUtil import *
from model.StockBaseInfo import *
from frameworks.utils.PrettyTableUtil import *
from frameworks.cores.LoadClass import *
import time

loader = LoadClass()
redis = RedisUtil()

def aacharts_set_cache():
    ck = "zhangting_cache"
    zhangtingService = ZhangTingService()
    baseinfo = StockBaseInfo()
    stocks = zhangtingService.getOneZhangting()
    daytime = datetime.datetime.now().strftime("%H%M%S")
    allrs = []
    for row in stocks:
        code = row["code"][2:]
        if row["code"][2:3] == "3" or row["code"][2:4] == "68":
            continue
        nowinfo = baseinfo.getDfcfInfo(code)
        print(nowinfo)
        # f43 当前价 f44 最高价 f45 最低价 f46开盘价  f47总手数 f48成交额 f50量比 f51涨停价 f52跌停价 f58证券名称 f71均价 f84总股本 f85流通股本
        # f116总市值 f117流通市值 f127综合行业，f128板块 f163市盈率，f168换手率
        # f169涨跌额 f170涨幅
        nowtime = datetime.datetime.now().strftime("%H%M%S")
        try:
            priceinfo = {
                "curtime": nowtime,
                "code": code,
                "close": float(nowinfo["f43"]),
                "high": float(nowinfo["f44"]),
                "low": float(nowinfo["f45"]),
                "open": float(nowinfo["f46"]),
                "zongshoushu": float(nowinfo["f47"]),
                "money": float(nowinfo["f48"]),
                "liangbi": float(nowinfo["f50"]),
                "zhangtingjia": float(nowinfo["f51"]),
                "dietingjia": float(nowinfo["f52"]),
                "codename": nowinfo["f58"],
                "junjia": float(nowinfo["f71"]),
                "zongshizhi": float(nowinfo["f116"]),
                "zhangfu": float(nowinfo["f170"]),
                "zhangdiee": float(nowinfo["f169"]),
                "liutongshizhi": float(nowinfo["f117"]),
                "zhonghehangye": nowinfo["f127"],
                "bankuai": nowinfo["f128"],
                "shiyinlv": float(nowinfo["f163"]),
                "huanshoulv": float(nowinfo["f168"])
            }
        except:
            info = {}
            info["subrate"] = float(nowinfo["f170"])
            allrs.append({
                "code": row["code"],
                "codename": row["codename"],
                "zhangfu": info["subrate"],
                "money": str(round(row["flow_money"] / 100000000)) + " 亿元",
                "industry": row["gainian"]
            })
            time.sleep(0.5)
            continue
        if nowinfo == False:
            continue
        ck1 = "zhangting_cache_" + code
        if int(nowtime) > 145500:
            redis.delete(ck1)
            continue
        print(code)
        data = redis.vget(ck1)
        print(data)
        if data != None:
            olddata = []
            olddata = json.loads(data)
            olddata.append(priceinfo)
            redis.vset(ck1, json.dumps(olddata))
        else:
            newdata = []
            newdata.append(priceinfo)
            redis.vset(ck1, json.dumps(newdata))
        info = {}
        info["subrate"] = float(nowinfo["f170"])
        allrs.append({
            "code": row["code"],
            "codename": row["codename"],
            "zhangfu": info["subrate"],
            "money": str(round(row["flow_money"] / 100000000)) + " 亿元",
            "industry": row["gainian"]
        })
        time.sleep(0.5)
    print(allrs)
    redis.vset(ck, json.dumps(allrs))
aacharts_set_cache()
